<?php

if (!defined('BASEPATH'))
    exit('No direct script access allowed');

/**
 * Generate controller
 */
class Generate extends SM_ADMIN_Controller
{

    /**
    * Check the module
    */
    function index()
    {
        $dir = APPPATH . 'modules/';
        $data = array();
        if ($handle = opendir($dir)) {
            while (false !== ($entry = readdir($handle))) {
                if ($entry != "." && $entry != ".." && $entry != ".svn") {
                    if (is_dir($dir . "/" . $entry) === true) {
                        $dir_controller = $dir . "/" . $entry . '/controllers/';
                        if ($handle_dir = opendir($dir_controller)) {
                            while (false !== ($controller = readdir($handle_dir))) {
                                if (preg_match('/.php/', $controller)) {
                                    require_once ($dir_controller . $controller);
                                    $controller_name = basename($controller, '.php');
                                    $reflector = new ReflectionClass($controller_name);
                                    $methods = $reflector->getMethods(ReflectionMethod::IS_PUBLIC);
                                    foreach ($methods as $method) {
                                        if ($method->name != '__construct'
                                                && $controller_name == strtolower($method->class)) {
                                            $data[$entry][$controller_name][] = $method->name;
                                        }
                                    }
                                }
                            }
                            closedir($handle_dir);
                        }
                    }
                }
            }
            closedir($handle);
        }

        $this->load->model('module_model');
        $this->module_model->deleteAll();
        if (!empty($data)) {
            foreach ($data as $key => $value) {
                $arrModule = array(
                    'module_name' => $key,
                    'controller_action' => json_encode($value)
                );
                $this->module_model->addModule($arrModule);
            }
        }
        $this->load->view('layout/main', array('content' => 'generate/index', 'data' => array()));
    }

}
